﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Lapas.Common.Forms;
using System.Drawing;
using System.Globalization;
using System.Security.Cryptography;

namespace Lapas.Common
{
    public partial class Helper
    {
        #region Security helper
        public static string Encrypt(string sourceString)
        {
            UnicodeEncoding encode = new UnicodeEncoding();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(encode.GetBytes(sourceString));
            return BitConverter.ToString(result).Replace("-", "");
        }
        public static string Generate(int len)
        {
            byte[] b = new byte[len];
            new RNGCryptoServiceProvider().GetBytes(b);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < b.Length; i++)
            {
                sb.Append(string.Format("{0:X2}", b[i]));
            }
            return sb.ToString();
        }
        /// <summary>
        /// Thuc hien bo het tat ca cac ky tu khong phai la so
        /// </summary>
        /// <param name="s"></param>
        public static string GetNumberOnly(string s)
        {
            string result = string.Empty;
            for (int i = 0; i < s.Length; i++)
            {
                if ('0' <= s[i] && s[i] <= '9')
                {
                    result += s[i];
                    if (result.Length == 8) { break; }
                }
            }
            return result;
        }
        public static string GenerateNumber(int len)
        {
            return GetNumberOnly(Generate(len));
        }
        
        #endregion
    }
}
